<?php

class product{


    protected $db;
    protected $pagingClass;
    protected $pageNumber;

    function  __construct($db,$pagingClass){
        $this->db = $db;
        $this->pagingClass = $pagingClass;
    }

    function getLatestProduct($category=null, $subcategory=null, $Search=null, $myProduct=null, $detail=null){        
        $row_perpage = ROW_PER_PAGE;
        $start_page  = $this->pagingClass->limit_page($row_perpage);
        $total_rows  = 0;

        if($Search != null){
            $query  = $this->db->query(
                    "SELECT product.*, subcategory.*, category.* FROM product
                    LEFT JOIN product_image ON product.product_id=product_image.product_image_id
                    INNER JOIN subcategory ON product.subcategory_id=subcategory.subcategory_id
                    INNER JOIN category ON subcategory.category_id=category.category_id
                    WHERE product.product_title LIKE '%$Search%'
                    GROUP BY product.product_id
                    ORDER BY product.product_submitdate"
                );
            $total_rows  = $this->db->count($query);
        }else if($category != null){            
            $query  = $this->db->query(
                    "SELECT product.*, subcategory.*, category.* FROM product
                    LEFT JOIN product_image ON product.product_id=product_image.product_image_id
                    INNER JOIN subcategory ON product.subcategory_id=subcategory.subcategory_id
                    INNER JOIN category ON subcategory.category_id=category.category_id
                    WHERE product.category_id=$category
                    GROUP BY product.product_id
                    ORDER BY product.product_submitdate"
                );
            $total_rows  = $this->db->count($query);
        }else if($subcategory != null){
            
            $query  = $this->db->query(
                    "SELECT product.*, subcategory.*, category.* FROM product
                    LEFT JOIN product_image ON product.product_id=product_image.product_image_id
                    INNER JOIN subcategory ON product.subcategory_id=subcategory.subcategory_id
                    INNER JOIN category ON subcategory.category_id=category.category_id
                    WHERE product.subcategory_id=$subcategory
                    GROUP BY product.product_id
                    ORDER BY product.product_submitdate"
                );
            $total_rows  = $this->db->count($query);
            
        }else if($myProduct != null ){
            $query  = $this->db->query(
                    "SELECT product.*, subcategory.*, category.* FROM product
                    LEFT JOIN product_image ON product.product_id=product_image.product_image_id
                    INNER JOIN subcategory ON product.subcategory_id=subcategory.subcategory_id
                    INNER JOIN category ON subcategory.category_id=category.category_id
                    WHERE product.member_id=$myProduct
                    GROUP BY product.product_id
                    ORDER BY product.product_submitdate DESC"
                );
            $total_rows  = $this->db->count($query);
        }else if($detail != null){
            $query  = $this->db->query(
                    "SELECT product.*, subcategory.*, category.* FROM product
                    LEFT JOIN product_image ON product.product_id=product_image.product_image_id
                    INNER JOIN subcategory ON product.subcategory_id=subcategory.subcategory_id
                    INNER JOIN category ON subcategory.category_id=category.category_id
                    WHERE product.product_id=$detail
                    GROUP BY product.product_id
                    ORDER BY product.product_submitdate DESC LIMIT 10"
                );
            $total_rows  = $this->db->count($query);
        }else{
            $query  = $this->db->query(
                    "SELECT product.*, subcategory.*, category.* FROM product
                    LEFT JOIN product_image ON product.product_id=product_image.product_image_id
                    INNER JOIN subcategory ON product.subcategory_id=subcategory.subcategory_id
                    INNER JOIN category ON subcategory.category_id=category.category_id
                    GROUP BY product.product_id
                    ORDER BY product.product_submitdate DESC LIMIT 10"
                );            
        }



        if($total_rows>$row_perpage){
            $link = '';
            if(isset($_GET['cat'])){
                $link = "productlist.php?cat=".$_GET['cat']."&page=";
            }else if(isset($_GET['subcat'])){
                $link = "productlist.php?subcat=".$_GET['subcat']."&page=";
            }else if(isset($_GET['s'])){
                $link = "productlist.php?s=".$_GET['s']."&page=";
            }else if($myProduct != null){
                $link = "myproduct.php?page=";
            }else{
                $link = "productlist.php?page=";
            }
            $this->pageNumber = $this->pagingClass->break_pagenum($total_rows,$row_perpage,$link);
        }

        if($Search != null){
            $query  = $this->db->query(
                    "SELECT product.*, subcategory.*, category.* FROM product
                    LEFT JOIN product_image ON product.product_id=product_image.product_image_id
                    INNER JOIN subcategory ON product.subcategory_id=subcategory.subcategory_id
                    INNER JOIN category ON subcategory.category_id=category.category_id
                    WHERE product.product_title LIKE '%$Search%'
                    GROUP BY product.product_id
                    ORDER BY product.product_submitdate DESC LIMIT $start_page, $row_perpage"
                );
        }else if($category != null){
            $query  = $this->db->query(
                    "SELECT product.*, subcategory.*, category.* FROM product
                    LEFT JOIN product_image ON product.product_id=product_image.product_image_id
                    INNER JOIN subcategory ON product.subcategory_id=subcategory.subcategory_id
                    INNER JOIN category ON subcategory.category_id=category.category_id
                    WHERE product.category_id=$category
                    GROUP BY product.product_id
                    ORDER BY product.product_submitdate DESC LIMIT $start_page, $row_perpage"
                );

        }else if($subcategory != null){
            $query  = $this->db->query(
                    "SELECT product.*, subcategory.*, category.* FROM product
                    LEFT JOIN product_image ON product.product_id=product_image.product_image_id
                    INNER JOIN subcategory ON product.subcategory_id=subcategory.subcategory_id
                    INNER JOIN category ON subcategory.category_id=category.category_id
                    WHERE product.subcategory_id=$subcategory
                    GROUP BY product.product_id
                    ORDER BY product.product_submitdate DESC LIMIT $start_page, $row_perpage"
                );
        }else if($myProduct != null ){
            $query  = $this->db->query(
                    "SELECT product.*, subcategory.*, category.* FROM product
                    LEFT JOIN product_image ON product.product_id=product_image.product_image_id
                    INNER JOIN subcategory ON product.subcategory_id=subcategory.subcategory_id
                    INNER JOIN category ON subcategory.category_id=category.category_id
                    WHERE product.member_id=$myProduct
                    GROUP BY product.product_id
                    ORDER BY product.product_submitdate DESC LIMIT $start_page, $row_perpage"
                );
        }else if($detail != null ){
            
            $query  = $this->db->query(
                    "SELECT product.*, subcategory.*, category.* FROM product
                    LEFT JOIN product_image ON product.product_id=product_image.product_image_id
                    INNER JOIN subcategory ON product.subcategory_id=subcategory.subcategory_id
                    INNER JOIN category ON subcategory.category_id=category.category_id
                    WHERE product.product_id=$detail
                    GROUP BY product.product_id
                    ORDER BY product.product_submitdate DESC LIMIT $start_page, $row_perpage"
                );
        }else{
            $query  = $this->db->query(
                    "SELECT product.*, subcategory.*, category.* FROM product
                    LEFT JOIN product_image ON product.product_id=product_image.product_image_id
                    INNER JOIN subcategory ON product.subcategory_id=subcategory.subcategory_id
                    INNER JOIN category ON subcategory.category_id=category.category_id
                    GROUP BY product.product_id
                    ORDER BY product.product_id DESC LIMIT 10"
                );
        }

        
        $ad_str = null;
		$ad_str = '<script type="text/javascript" src="lightbox.js"></script>';
		$ad_str = '<link rel="stylesheet" href="lightbox.css" type="text/css" />';
		$ad_str .= '<table width="100%">';
        $i = 0;
        while($row=$this->db->fetch($query)){
            $i++;
			$ad_str .= '</table>';
			$ad_str .= '<table width="100%">';
            //$ad_str .= '<tr>';
            $ad_str .= '<tr><td colspan=:3"><h3>'.$row['product_title'].'</h3></td></tr>';
			$ad_str .= '</table>';
			$ad_str .= '<table width="100%">';
			
            $ad_str .= '<td colspan="3"><table><tr>';
            if(!isset($_GET['detail'])){
                $query1 = $this->db->query("SELECT * FROM product_image WHERE product_id=".$row['product_id']." LIMIT 1");
            }else{
                $query1 = $this->db->query("SELECT * FROM product_image WHERE product_id=".$row['product_id']);
            }
			
			
            //echo "SELECT * FROM product_image WHERE product_id=".$row['product_id'];
            while($row2 = $this->db->fetch($query1)){
                $ad_str .= '<td><a rel="lightbox" href="images/'.$row2['product_image_name'].'"><img src="images/'.$row2['product_image_name'].'" width="100" align="left" /></a></td>';
            }
            $ad_str .= '</tr></table></td>';
            $ad_str .= '<tr>';
            $ad_str .= '<tr><td width="150"></td><td width="50"></td><td></td></tr>';
            $ad_str .= '<tr><td>Posted Date</td><td>:</td><td>'.$row['product_submitdate'].'</td></tr>';
            $ad_str .= '<tr><td>Expire Date</td><td>:</td><td>'.$row['product_expiredate'].'</td></tr>';
            $ad_str .= '<tr><td>Price</td><td>:</td><td>USD '.$row['product_price'].'</td></tr>';
            $ad_str .= '<tr><td valign="top">Description</td><td valign="top">:</td><td>'.$row['product_description'].'</td></tr>';
            if(!isset($_GET['detail'])){
                $ad_str .= '<tr><td valign="top"></td><td valign="top"></td><td><a href="productlist.php?detail='.$row['product_id'].'">detail...</a></td></tr>';
            }
            if($myProduct!=null){
                //$ad_str .= '<tr><td valign="top"></td><td valign="top"></td><td><a href="editproduct.php?update='.$row['product_id'].'">update...</a> | <a href="editproduct.php?delete='.$row['product_id'].'">delete...</a></td></tr>';
            }
            $ad_str .= '<tr><td colspan="3"><hr size="1" color="#f0f0f0"/></td></tr>';
            if(isset($_GET['detail'])){
                $ad_str .= '<tr><td colspan="3"><div id="commentbody"><table width="100%" class="commentbody">';
                $comment_query = $this->db->query("SELECT member.*, comment.* FROM comment INNER JOIN member ON member.member_id=comment.member_id WHERE comment.product_id=$detail ORDER BY comment.comment_date DESC");
                while($comment_row = $this->db->fetch($comment_query)){
                    $ad_str .= '<tr><td><b>'.$comment_row['member_username'].'</b> <small>'.$comment_row['comment_date'].'<small></td></tr>';
                    $ad_str .= '<tr><td>'.$comment_row['comment_description'].'</td></tr>';
                    $ad_str .= '<tr><td style="border-bottom: 1px dotted #e0e0e0;"></td></tr>';
                }
                $ad_str .= '</table></td></tr>';
                $ad_str .= '<tr><td colspan="3"><textarea id="txtcomment" name="txtcomment" style="width: 400px; height: 100px;" class="rounded_corner"></textarea></td></tr>';
                $ad_str .= '<tr><td><input type="hidden" id="hdnproductid" value="'.$detail.'" /><input type="button" class="rounded_corner" id="btncomment" name="btncomment" value="Comment" style="cursor:pointer;"/></td></tr>';
            }
        }
        if($i==0){
            $ad_str .= '<tr><td colspan="3" style="text-align: center;"><font color="#FF0000"><b>Product not found!</b></font></td></tr>';
        }
        $ad_str .= '</table>';
        return $ad_str;
    }

    function getPageNumber(){
        return $this->pageNumber;
    }
}
?>
